var userId;
$(function () {
    new QWebChannel(qt.webChannelTransport, function (channel) {
        window.addFriendObject = channel.objects.addFriendObject;
        console.log(addFriendObject);
    })

    $('#search-input').on('keydown', function (e) {

        var friendId = $('#search-input').val();
        if (friendId != '') {
            try {
                var id = parseInt(friendId)
                if (e.keyCode == 13) {
                    $('#tip').html('');
                    addFriendObject.searchFriend(id,function(data){
                        console.log(data);
                        var result = JSON.parse(data)
                        if(result.code == 0){
                            var userInfo = result.data;
                            userId = userInfo.id;
                            if(userInfo != undefined){
                                $('#avatar').attr('src',userInfo.avatar)
                                $('#username').html(userInfo.username)
                                $('#userInfo-div').show()
                            }
                        }

                    })
                }
            } catch (e) {
                return
            }

        }
    })

    $('#add-btn').on('click',function () {
        $('#tip').html('');
        $('#extra').val('');
        addFriendObject.checkIsFriend(userId,function(result){
            if(result == 0){
                $('#tip').html("对方已经是你的好友");
            }else{
                $('#hover_div').show();
                $('#extra_window').show();
            }
        })
    })

    $('#cancel').on('click',function () {
        $('#hover_div').hide();
        $('#extra_window').hide();
        $('#extra').val('');
    })

    $('#confirm').on('click',function () {
        var extra = $('#extra').val();
        addFriendObject.add(userId,extra,function(result){
            $('#hover_div').hide();
            $('#extra_window').hide();
            //var r = JSON.parse(result)
            $('#tip').html(result);
        })
    })


})